package com.burningtower.service.impl;

import com.burningtower.entity.User;
import com.burningtower.mapper.UserMapper;
import com.burningtower.service.IOrganizationService;
import com.burningtower.service.IRoleService;
import com.burningtower.service.IUserRoleService;
import com.burningtower.service.IUserService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.burningtower.vo.UserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author zhe
 * @since 2017-11-17
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private IOrganizationService organizationService;
    @Autowired
    private IUserRoleService userRoleService;
    @Autowired
    private IRoleService roleService;

    @Override
    public UserVo toVo(User user) {
        UserVo vo=new UserVo();
        BeanUtils.copyProperties(user,vo);
        vo.setOrganizationName(organizationService.selectNameById(Long.valueOf(user.getOrganizationId())));
        Long[] roleIds = userRoleService.selectRoleIdByUserId(user.getId());
        String roles="";
        for(Long roleId:roleIds){
            roles+=roleService.selectById(roleId).getName()+",";
        }
        vo.setRoleList(roles.length()>1?roles.substring(0,roles.length()-1):"");
        return vo;
    }
}
